package com.ylb.rent.service.impl;

import com.ylb.rent.dao.UserDao;
import com.ylb.rent.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.UserDetailsManagerConfigurer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/6/28 9:25
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    
    @Autowired
    UserDao userDao;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.selectByUserName(username);
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<GrantedAuthority> authorityList =new ArrayList<>();
        return org.springframework.security.core.userdetails.User.builder()
                .username(user.getUsername())
                .password(user.getPassword())
                .accountExpired(false)  //证书
                .accountLocked(false)   //
                .credentialsExpired(false)
                .authorities(authorityList) //权限
                .build();
    }
}
